local bCountItemPush=0;
local tCountItemPushNum={};
local beginDateTime = {};
g_bUseBait = 0;
g_bShowRate = 0;

function IsCountItemPush()
	return 	bCountItemPush;
end

function SwitchBaitUse()
	if 1 == g_bUseBait then
		g_bUseBait = 0;
	else
		g_bUseBait = 1;
	end
	MyTricksInfo("SwitchBaitUse() g_bUseBait="..g_bUseBait);
end

local function TranslateIcon2ItemName(bagId,icon)	
	MyTricksLog("TranslateIcon2ItemName() bagId="..bagId);
	for bagId = 0,4,1 do
		for i = 1,22,1 do		
			local link = GetContainerItemLink(bagId, i);
			if nil ~= link then
				local name, link2, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice=GetItemInfo(link);
				if texture == icon then
					return link2;
				end
			end
		end
	end
	return "未知";
end

function HandItemPushEvent(self,event,...)
	local arg={};
	if bCountItemPush > 0 then
		for i = 1, select('#', ...) do
			arg[i]=select(i, ...);
    end
	  tCountItemPushNum["总计"]=tCountItemPushNum["总计"] + 1;
	  local sItemName=TranslateIcon2ItemName(arg[1],arg[2]);
		if tCountItemPushNum[sItemName] == nil then
			tCountItemPushNum[sItemName] = 1;
		else
			tCountItemPushNum[sItemName] = tCountItemPushNum[sItemName] + 1 ;
		end
		local endDateTime={};
		local iMinuteCount;
		endDateTime.weekday,endDateTime.month,endDateTime.day,endDateTime.year=CalendarGetDate();
		endDateTime.hour,endDateTime.minute=GetGameTime();
		if beginDateTime.year ~= endDateTime.year or beginDateTime.month ~= endDateTime.month then
			iMinuteCount="未知";			
		else
			iMinuteCount=((endDateTime.day-beginDateTime.day)*24+endDateTime.hour-beginDateTime.hour)*60+endDateTime.minute-beginDateTime.minute;
		end 
		if mod(tCountItemPushNum["总计"],10) == 0 then
			MyTricksInfo("获取第"..tCountItemPushNum["总计"].."件物品,物品是["..sItemName.."],过去了"..iMinuteCount.."分钟");
		end
		MtEncode(30001);
	end
end

local function CreateAutoUseBaitMacro()
	local index=GetMacroIndexByName("AutoUseBait");
	if 0 ~= index then
		DeleteMacro("AutoUseBait");
	end
	local sMarcroBody="";
	
	for i = 0,4,1 do
		for j = 1,22,1 do
			local link = GetContainerItemLink(i, j);
			if nil ~= link then
				local name, link2, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice=GetItemInfo(link);
				MyTricksLog("CreateAutoUseBaitMacro() find item:"..name);
				if name == "夜色虫" or name == "明亮的小珠" or name=="水下诱鱼器" or name=="锐利的鱼钩" then
					local index=GetMacroIndexByName("AutoUseBait");
					if 0 ~= index then
						DeleteMacro("AutoUseBait");
					end
					sMarcroBody="/script UseContainerItem("..i..","..j..")\n/script PickupInventoryItem(16)"; 
					CreateMacro("AutoUseBait", 92, sMarcroBody, 1,nil);
					SetBindingMacro("F11","AutoUseBait");
					return;
				end
			end	
		end
	end
	
	MyTricksErr("couldn't find any bait!");
end

local function BeginCountItemPush()
	MtCodeFrm:Show();
	bCountItemPush = 1;
	tCountItemPushNum=nil;
	tCountItemPushNum={};
	tCountItemPushNum["总计"] = 0;
	beginDateTime.weekday,beginDateTime.month,beginDateTime.day,beginDateTime.year=CalendarGetDate();
	beginDateTime.hour,beginDateTime.minute=GetGameTime();
	MyTricksInfo("BeginCountItemPush time "..beginDateTime.hour..":"..beginDateTime.minute);
	
	local index=GetMacroIndexByName("CheckBait");
	if 0 == index then
		CreateMacro("CheckBait", 91, "/run CheckBait()", 1,nil);
	end
	SetBindingMacro("F9","CheckBait");
	
	index=GetMacroIndexByName("ShowRate");
	if 0 == index then
		CreateMacro("ShowRate", 93, "/run g_bShowRate=1;", 0,nil);
	end
	
	CheckBait();
end

function CheckBait()
	local msg=0;
	MyTricksLog("CheckBait()");
	if g_bShowRate == 1 then
		msg=10002;
		MtEncode(msg);
		g_bShowRate = 0;
		return;
	end
	local name, rank, icon, count, dispelType, duration, expires, caster, isStealable, shouldConsolidate, spellID;
	if 0 ~= g_bUseBait then
		for skillIndex = 1, GetNumSkillLines() do
		  local skillName, isHeader, isExpanded, skillRank, numTempPoints, skillModifier,
		    skillMaxRank, isAbandonable, stepCost, rankCost, minLevel, skillCostType,
		    skillDescription = GetSkillLineInfo(skillIndex)
		  if "钓鱼" == skillName  then
		  	MyTricksLog("CheckBait() fishing numTempPoints="..numTempPoints..",skillModifier="..skillModifier..",skillRank="..skillRank);
		    if skillModifier > 30 then 
		    	msg=0;
		    	MtEncode(msg);
		    	return;
		    else
		    	CreateAutoUseBaitMacro();
					msg = 10001;
					MtEncode(msg);
					return;
		  	end
		  end
		end	
	end
	MtEncode(msg);
end

function ShowItemPushStatistic()
	local endDateTime={};
	endDateTime.weekday,endDateTime.month,endDateTime.day,endDateTime.year=CalendarGetDate();
	endDateTime.hour,endDateTime.minute=GetGameTime();
	if beginDateTime.year ~= endDateTime.year or beginDateTime.month ~= endDateTime.month then
		return;		
	end 
	local iMinuteCount=((endDateTime.day-beginDateTime.day)*24+endDateTime.hour-beginDateTime.hour)*60+endDateTime.minute-beginDateTime.minute;
	if iMinuteCount == 0 then
		iMinuteCount = 1;
	end
	
	MyTricksInfo("物品名    总数量    每分钟数量");
	for sItemName,iCount in pairs(tCountItemPushNum) do
		if sItemName ~= "总计" then
			MyTricksInfo(sItemName.."    "..iCount.."    "..iCount/iMinuteCount);
		end
	end	
	MyTricksInfo("总 计    "..tCountItemPushNum["总计"].."    "..tCountItemPushNum["总计"]/iMinuteCount.." 用时 "..floor(iMinuteCount/60)..":"..mod(iMinuteCount,60));
end

local function StopCountItemPush()
	MtCodeFrm:Hide();
	bCountItemPush = 0;
	ShowItemPushStatistic();
end



SLASH_COUNTITEMPUSH1 = "/countitempush"
SlashCmdList["COUNTITEMPUSH"] = BeginCountItemPush

SLASH_NOCOUNTITEMPUSH1 = "/nocountitempush"
SlashCmdList["NOCOUNTITEMPUSH"] = StopCountItemPush

